$(document).ready(function() {
	ProductDeatail.init_page();
	ProductDeatail.ClickBuyNow();
	ProductDeatail.ClickAddCar();
})
//vue
var v_product_buy = new Vue({
	el: '.v_product_buy',
	data: function() {
		return {
			product_photo_lg: '',
			product_photo_sm: [],
			product_buy_take: {},
			active: '-1',
			product_dispatch: [], //地区列表
			product_sele_spec: [], //规格
			goods_store_nums: 0, //库存数量
			num: 1, //购买数量
			areaName: '',
			transport_id: '',
			sprice: ''
		}
	},
	methods: {
		clickShow: function(index, img_url, e) {
			this.product_photo_lg = img_url;
			this.active = index;
		},
		num_minus: function() {
			var that = this;
			that.num--;
			if(that.num < 1) {
				that.num = 1;
			}
		},
		num_add: function() {
			var that = this;
			that.num++;
		},
		changeArea: function() {
			var that = this
			console.log('that.areaName', that.areaName)
			$.ajax({
				type: "post",
				url: "http://www.ibabypet.com.cn/public/index.php/api/goods/yunfei/",
				async: true,
				data: {
					top_area_id: that.areaName,
					transport_id: that.transport_id
				},
				success: function(res) {
					res = JSON.parse(res);
					console.log('根据地区获取运费', res)
					that.sprice = res.data.transport[0].sprice
				},
				error: function(res) {
					
				}
			});

		}
	}
})
//tab 
var product_detail_tabs = new Vue({
	el: '#v_product_detail_tabs',
	data: function() {
		return {
			activeName: 'first',
			product_detail: {}
		}
	},
	methods: {
		handleClick: function(tab, event) {
			console.log(tab, event)
		}
	}
})

var ProductDeatail = {
	//接口
	url_api: 'http://www.ibabypet.com.cn/public/index.php/api/goods/goods_details', //获取商品详情
	url_api2: 'http://www.ibabypet.com.cn/public/index.php/api/goods/format/', //商品规格 传的参数goods_id(商品id)
	url_api3: 'http://www.ibabypet.com.cn/public/index.php/api/area/area_list', //配送地区列表
	url_api4: 'http://www.ibabypet.com.cn/public/index.php/api/goods/add_cart/', //添加到购物车 uid(用户uid),goods_id(商品id),format_id(规格id),num(商品数量)
	//商品id
	goods_id: window.location.search.split('=')[1],
	//获取cookie
	getCookie: function(cname) {
		var name = cname + "=";
		var ca = document.cookie.split(';');
		for(var i = 0; i < ca.length; i++) {
			var c = ca[i];
			while(c.charAt(0) == ' ') c = c.substring(1);
			if(c.indexOf(name) != -1) return c.substring(name.length, c.length);
		}
		return "";
	},
	//根据上一个一面传递过来商品id ajax获取数据
	init_page: function() {
		var that = this;
		var url_search = window.location.search;
		var goods_id = url_search.split('=')[1];
		console.log('goods_id', goods_id);
		var p_data = {};
		p_data = {
			id: goods_id
		};
		$.ajax({
			type: "post",
			url: that.url_api,
			async: true,
			data: p_data,
			success: function(res) {
				res = JSON.parse(res);
				console.log('详情页面获取到的数据res', res)
				if(res.code == 200) {
					v_product_buy.product_photo_lg = res.data.album[0].img;
					v_product_buy.product_photo_sm = res.data.album;
					v_product_buy.product_buy_take = res.data.goods;
					product_detail_tabs.product_detail = res.data.goods;
					v_product_buy.goods_store_nums = res.data.goods.num;
					v_product_buy.transport_id = res.data.goods.transport_id

				} else {
				}
			},
			error: function(res) {
			}
		});
		//商品配送地区
		$.ajax({
			type: "get",
			url: that.url_api3,
			async: true,
			success: function(res) {
				res = JSON.parse(res);
				console.log('地区res', res)
				if(res.code == 200) {
					var area_arr = res.data.area;
					v_product_buy.product_dispatch = area_arr;
				} else {
				}
			},
			error: function(res) {
			}
		});
		//商品规格
		$.ajax({
			type: "post",
			url: that.url_api2,
			async: true,
			data: {
				goods_id: that.goods_id
			},
			success: function(res) {
				res = JSON.parse(res);
				console.log('商品规格res', res)
				if(res.code == 200) {
					v_product_buy.product_sele_spec = res.data;
				} else {
				}
			},
			error: function(res) {
			}
		});
	},
	//点击立即购买
	ClickBuyNow: function() {
		$('.buy_now').on('click', function() {
			$('.payment_modal, .payment_scan_wrap').show();
		});
		$('.payment_modal').on('click', function() {
			$('.payment_modal, .payment_scan_wrap').hide();
		})
	},
	//点击加入购物车
	ClickAddCar: function() {
		var that = this;
		$('.add_car').on('click', function() {
			var uid = that.getCookie('uid')
			var goods_id, format_id, num;
			goods_id = that.goods_id;
			//规格
			format_id = $.trim($('.product_sele_spec input:radio:checked').val());
			num = $.trim($('.product_sele_num input').val());
			var data = {};
			data = {
				uid: uid,
				goods_id: goods_id,
				format_id: format_id,
				num: num
			};
			$.ajax({
				type: "post",
				url: that.url_api4,
				async: true,
				data: data,
				success: function(res) {
					res = JSON.parse(res);
					console.log('加入购物车res', res)
					if(res.code == 200) {
						var M = {}
						if(M.dialog){
							return M.dialog.show()
						}
						M.dialog = jqueryAlert({
							'content': res.msg,
							'closeTime': 2000
						})
						setTimeout(function(){
							window.location.href = 'http://www.ibabypet.com.cn/pcpet/shopCar.html';
						}, 1500)
					}
				}
			});
		})
	}
}